package cn.getech.data.metadata.service.impl;

import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PageUtils2;
import cn.getech.data.metadata.entity.CollectionRoleDataPermission;
import cn.getech.data.metadata.mapper.CollectionTableRoleDataPermissionMapper;
import cn.getech.data.metadata.model.req.roledatapermission.CollectionTableRoleDataPermissionListReq;
import cn.getech.data.metadata.model.req.roledatapermission.CollectionTableRoleDataPermissionSaveInner;
import cn.getech.data.metadata.model.req.roledatapermission.CollectionTableRoleDataPermissionSaveReq;
import cn.getech.data.metadata.model.res.roledatapermission.CollectionTableRoleDataPermissionRes;
import cn.getech.data.metadata.service.CollectionTableRoleDataPermissionService;
import cn.getech.system.center.entity.SysRoleEntity;
import cn.getech.system.center.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("collectionTableRoleDataPermissionService")
public class CollectionTableRoleDataPermissionServiceImpl extends ServiceImpl<CollectionTableRoleDataPermissionMapper, CollectionRoleDataPermission> implements CollectionTableRoleDataPermissionService {
    @Autowired
    private SysRoleService sysRoleService;

    @Override
    public PageUtils2<CollectionTableRoleDataPermissionRes> list(CollectionTableRoleDataPermissionListReq req) {
        Map<String, Object> params = new HashMap();
        if (StringUtils.isNotEmpty(req.getName())) {
            params.put("name", req.getName());
        }

        if (req.getLimit() != null && req.getPage() != null) {
            params.put("offset", (req.getPage() - 1) * req.getLimit());
            params.put("limit", req.getLimit());
        }

        List<CollectionTableRoleDataPermissionRes> list = baseMapper.collectionTableRoleDataPermissionList(params);
        int count = baseMapper.collectionTableRoleDataPermissionCount(params);
        PageUtils2<CollectionTableRoleDataPermissionRes> pageUtils = new PageUtils2(list, count, req.getLimit(), req.getPage());
        return pageUtils;
    }

    @Override
    public void save(CollectionTableRoleDataPermissionSaveReq req) {
        SysRoleEntity roleEntity = sysRoleService.getById(req.getRoleId());
        if(roleEntity == null)
            throw new RRException("没有该角色");

        List<CollectionRoleDataPermission> list = new ArrayList<>();
        for(CollectionTableRoleDataPermissionSaveInner inner : req.getPermissioins()){
            CollectionRoleDataPermission dataPermission = new CollectionRoleDataPermission();
            BeanUtils.copyProperties(inner, dataPermission);
            dataPermission.setRoleId(req.getRoleId());
            dataPermission.setDataType(5);//1 数据源 ，2 数据仓库表，3数据仓库表字段，4数据服务,5元数据管理
            dataPermission.setCreate(0);
            dataPermission.setExport(0);
            dataPermission.setDecrypt(0);
            list.add(dataPermission);
        }
        saveBatch(list);
    }
}
